import os

def count_lines_in_file(file_path):
    """统计单个文件的代码行数"""
    with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
        lines = file.readlines()
    return len(lines)

def count_lines_in_directory(directory):
    """统计指定目录下所有 .py 文件的代码行数"""
    total_lines = 0
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.py'):
                file_path = os.path.join(root, file)
                lines = count_lines_in_file(file_path)
                total_lines += lines
                print(f"{file_path}: {lines} 行")
    return total_lines

if __name__ == "__main__":
    project_directory = input("请输入项目目录路径: ")
    total_lines = count_lines_in_directory(project_directory)
    print(f"\n项目总代码行数: {total_lines} 行")
